package us.pinguo.loader.core.listener;

import us.pinguo.loader.PinguoImageLoader;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;

/**
 * time: 15/6/11 
 * description:当控件(ListView)在滑动过程当中时暂停图片的加载，停止后恢复加载
 *
 * @author fandong
 */
public class PauseOnScrollListener implements OnScrollListener {
	private final boolean pauseOnScroll;
	private final boolean pauseOnFling;
	private final OnScrollListener externalListener;

	public PauseOnScrollListener(boolean pauseOnScroll, boolean pauseOnFling) {
		this(pauseOnScroll, pauseOnFling, (OnScrollListener) null);
	}

	public PauseOnScrollListener(boolean pauseOnScroll, boolean pauseOnFling, OnScrollListener customListener) {
		this.pauseOnScroll = pauseOnScroll;
		this.pauseOnFling = pauseOnFling;
		this.externalListener = customListener;
	}

	public void onScrollStateChanged(AbsListView view, int scrollState) {
		switch (scrollState) {
		case 0:
			PinguoImageLoader.resume();
			break;
		case 1:
			if (this.pauseOnScroll) {
				PinguoImageLoader.pause();
			}
			break;
		case 2:
			if (this.pauseOnFling) {
				PinguoImageLoader.pause();
			}
		}

		if (this.externalListener != null) {
			this.externalListener.onScrollStateChanged(view, scrollState);
		}

	}

	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		if (this.externalListener != null) {
			this.externalListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
		}

	}
}
